<?php

class ListController extends CommonController{
	
	public function index(){
		
		$arr=K('Category')->get_top_cid(Q('get.cid'));
		$top_cid=Q('get.cid');
		foreach ($arr as $k => $v) {
			if($v!=0){
				$top_cid=$v;
			}
		}
		$data=K('Category')->get_all_data_notree($top_cid);
		
		foreach ($data as $key => $value) {
			$data[$key]['son']=K('Category')->get_all_data_notree(array('pid'=>$value['cid']));
			foreach ($data[$key]['son'] as $k => $v) {
				$data[$key]['son'][$k]['son']=K('Category')->get_all_data_notree(array('pid'=>$v['cid']));
			}
		}
		
		$this->assign('data',$data);
		
		$son_cid=K('Category')->get_son_cid(Q('get.cid'));
		
		$son_cid[]=Q('get.cid');
		
		
		$where='cid in (' . implode(',', $son_cid) . ')';
		
		$gids=K('GDGView')->get_gid($where);
		
		
			if($gids){
				$attr=K('Gproperty')->where(array('gid in(' . implode(',', $gids) . ')'))->group('gpvalue')->all();
				
				$temp=array();
				foreach ($attr as $k => $v) {
					$temp[$v['tpid']][]=$v;
					
				}
//		p($gids);	
//		p($attr);
//		p($temp);	
				$attrFinal=array();
				
				foreach ($temp as $k => $v) {
					$attrFinal[]=array(
						'tpname'=>K('Tproperty')->get_tpname(array('tpid'=>$k)),
						'tpvalue'=>$v,
					);
				}
//			p($attrFinal);	
				$attrFinal2=$attrFinal;
				foreach ($attrFinal as $k => $v) {
					foreach ($attrFinal2 as $key => $value) {
						if($v['tpname']==$value['tpname'] && $v['tpvalue']!=$value['tpvalue']){
							$attrFinal[$k]['tpvalue']=array_merge($v['tpvalue'],$value['tpvalue']);
						}
					}
				}
				$temp_d = array();
//				p($attrFinal);
				foreach ($attrFinal as $key => $value) {
					$temp_d[$value['tpname']] = $value;
				}
				
				$attrFinal=array();
				foreach ($temp_d as $k => $v) {
					$attrFinal[]=$v;
				}
				
//				p($attrFinal);
				$this->assign('attrFinal',$attrFinal);
				
				$total=count($attrFinal);
				if($total){
					$param = isset($_GET['param'])?explode('_', $_GET['param']):array_fill(0, $total, 0);
//						p($param);
					$this->assign('param',$param);
				
					$a=array();
					foreach ($param as $k => $v) {
						if($v!=0){
							$a[]=$data =M()->join('__gproperty__ g1 JOIN __gproperty__ g2 ON g1.gpvalue=g2.gpvalue')->where(array('g1.gpid'=>$v))->getField('g2.gid',TRUE);
						}
						
					}
					if($a){
						$temp = $this->jiaoji($a);
//						p($temp);
						if($temp){
							
						$where .= ' and good.gid in (' . implode(',', $temp) . ')';
						}else{
							$where .= ' and good.gid in (0)';
						}
					}
				}
				
			}
		
		$good=K('GDGView')->get_all_data($where);
		
//		p($good);
		$this->assign('good',$good);
		
		$this->dis('List');
	}
	
	
	private function jiaoji($arr){
		
		$temp=$arr[0];
		foreach ($arr as $k => $v) {
			$temp=array_intersect($temp, $v);
		}
		return $temp;
		
	}
	
	
	
	
	
}
